package com.xsoft.demo12.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xsoft.demo12.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
            }
        });
        try {
            List<View> views = new ArrayList<>();
            ImageView imageView = new ImageView(this);
            imageView.setImageBitmap(BitmapFactory.decodeStream(getAssets().open("page1.jpg")));
            views.add(imageView);
            ImageView imageView1 = new ImageView(this);
            imageView1.setImageBitmap(BitmapFactory.decodeStream(getAssets().open("page2.jpg")));
            views.add(imageView1);
            ImageView imageView2 = new ImageView(this);
            imageView2.setImageBitmap(BitmapFactory.decodeStream(getAssets().open("page3.jpg")));
            views.add(imageView2);
            ImageView imageView3 = new ImageView(this);
            imageView3.setImageBitmap(BitmapFactory.decodeStream(getAssets().open("page4.jpg")));
            views.add(imageView3);
            viewPager.setAdapter(new MyPagerAdapter(views));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class MyPagerAdapter extends PagerAdapter {
        private List<View> views;

        public MyPagerAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            return views == null ? 0 : views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }
    }
}
